package gw.com.android.ui.home;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import gw.com.android.presenter.http.CalendarRequest;
import gw.com.android.ui.news.CalendarAdapter;
import gw.com.android.ui.news.CalendarArgs;
import www.com.library.interfaces.ReqCallBack;
import www.com.library.view.XRecyclerView;

/**
 * @author jett
 * @since 2018-11-09.
 */
class HomeCalendarPresenter {

    private LinearLayout llNoData;
    private LinearLayout llNoFilter;
    private XRecyclerView rvList;
    private ProgressBar pbLoading;
    private RelativeLayout rlHead;
    private TextView tvFilter;
    private ImageView ivFilter;
    private RelativeLayout rlPopup;

    private CalendarAdapter mAdapter;
    private CalendarArgs mArgs = new CalendarArgs();
    private CalendarArgs filterArgs;
    private List<CalendarRequest.CalendarData> mDatas;
    private CalendarHeadHolder mHeadHolder;

    private final Activity mActivity;
    private boolean inited = false;
    private boolean loadSuccess = false;

    HomeCalendarPresenter(Activity activity, View calendarView) {
        this.mActivity = activity;
        llNoData = calendarView.findViewById(R.id.calendar_no_data);
        llNoFilter = calendarView.findViewById(R.id.calendar_no_fileter);
        rvList = calendarView.findViewById(R.id.calendar_list);
        pbLoading = calendarView.findViewById(R.id.calendar_loading);
        rlHead = calendarView.findViewById(R.id.calendar_head);
        tvFilter = calendarView.findViewById(R.id.calendar_filter_text);
        ivFilter = calendarView.findViewById(R.id.calendar_filter_icon);
        rlPopup = calendarView.findViewById(R.id.calendar_popup);
        rvList.setRefreshFrozen(true);
        rvList.setScrollFrozen(true);
    }

    private void init(boolean frozen) {
        inited = true;
        rvList.setScrollFrozen(frozen);
        rvList.setLayoutManager(new LinearLayoutManager(mActivity));
        rvList.setLoadingMoreEnabled(false);
        mAdapter = new CalendarAdapter(mActivity);
        rvList.setAdapter(mAdapter);
        rvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                requestCalendar();
            }

            @Override
            public void onLoadMore() {
            }
        });
        mHeadHolder = new CalendarHeadHolder(mActivity, this, rlHead, rlPopup);
        // 头部过滤变动回调
        mHeadHolder.setCallback(new CalendarHeadHolder.CalendarHeadViewCallback() {
            @Override
            public void onRefresh(Calendar date) {
                filterArgs = null;
                mArgs.calendar = date;
                tvFilter.setTextColor(mActivity.getResources().getColor(R.color.color_v));
                ivFilter.setImageResource(R.mipmap.a_calendar_screen_stateless);
                rvList.setRefreshing(true);
            }

            @Override
            public void onFilter(CalendarArgs args) {
                filterArgs = args;
                refreshUi(mDatas, false);
            }
        });

        List<CalendarRequest.CalendarData> list = new CalendarRequest(null).fetchLocal();
        refreshUi(list, true);
    }

    public void onSelected(boolean frozen) {
        if (!inited) {
            init(frozen);
        }
        if (!loadSuccess) {
            rvList.setRefreshing(true);
        }
    }

    public void onRefresh() {
        if (inited) {
            rvList.smoothScrollToPosition(0);
            rvList.setRefreshing(true);
        }
    }

    public void setFrozen(boolean frozen) {
        rvList.setScrollFrozen(frozen);
    }

    private void refreshUi(List<CalendarRequest.CalendarData> list, boolean isCache) {
        boolean isFileter = filterArgs != null && !filterArgs.isEmpty();
        loadSuccess = !isCache;
        if (list == null || list.isEmpty()) {
            if (isCache) {
                return;
            }
            pbLoading.setVisibility(View.GONE);
            mAdapter.updateData(null);
            if (isFileter) {
                llNoData.setVisibility(View.GONE);
                llNoFilter.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.VISIBLE);
                llNoFilter.setVisibility(View.GONE);
            }
            return;
        }
        mDatas = list;
        pbLoading.setVisibility(View.GONE);
        llNoData.setVisibility(View.GONE);
        llNoFilter.setVisibility(View.GONE);
        mHeadHolder.resetFilterPopup();

        if (!isFileter) {
            tvFilter.setTextColor(mActivity.getResources().getColor(R.color.color_v));
            ivFilter.setImageResource(R.mipmap.a_calendar_screen_stateless);
            mAdapter.updateData(mDatas);
        } else {
            tvFilter.setTextColor(0xFF2A60F5);
            ivFilter.setImageResource(R.mipmap.a_calendar_screen_focus);
            List<CalendarRequest.CalendarData> calendarData = filterData(filterArgs);
            if (calendarData == null || calendarData.isEmpty()) {
                mAdapter.updateData(null);
                llNoData.setVisibility(View.GONE);
                llNoFilter.setVisibility(View.VISIBLE);
            } else {
                mAdapter.updateData(calendarData);
            }
        }
    }

    private void requestCalendar() {
        new CalendarRequest(mArgs).call(new ReqCallBack<List<CalendarRequest.CalendarData>>() {
            @Override
            public void onReqSuccess(List<CalendarRequest.CalendarData> result) {
                rvList.refreshComplete();
                refreshUi(result, false);
                rvList.setNoMore(true);
            }

            @Override
            public void onReqFailed(String errorMsg) {
                rvList.refreshComplete();
                rvList.setNoMore(true);

                //加载失败应该显示 占位图
                refreshUi(null, false);
            }
        });
    }

    /**
     * 本地数据筛选
     */
    private List<CalendarRequest.CalendarData> filterData(CalendarArgs args) {
        if (mDatas != null && !mDatas.isEmpty()) {
            List<CalendarRequest.CalendarData> result = new ArrayList<>();
            for (CalendarRequest.CalendarData data : mDatas) {
                if (data == null) {
                    continue;
                }
                if (args.countries != null && !args.countries.isEmpty()) {
                    if (!args.countries.contains(data.country)) {
                        continue;
                    }
                }
                if (CalendarArgs.STATUS_PRIVATE.equals(args.status)) {
                    if (!data.isFinanceData()) {
                        continue;
                    }
                    if (!TextUtils.isEmpty(data.value)) {
                        continue;
                    }
                } else if (CalendarArgs.STATUS_PUBLIC.equals(args.status)) {
                    if (!data.isFinanceData()) {
                        continue;
                    }
                    if (TextUtils.isEmpty(data.value)) {
                        continue;
                    }
                }
                if (args.level != null && !args.level.isEmpty()) {
                    if (!args.level.contains(String.valueOf(data.importanceLevel))) {
                        continue;
                    }
                }
                if (args.types != null && !args.types.isEmpty()) {
                    if (!args.types.contains(CalendarArgs.TYPE_DATA)) {
                        if (data.isFinanceData()) {
                            continue;
                        }
                    }
                    if (!args.types.contains(CalendarArgs.TYPE_EVENT)) {
                        if (data.isFinanceEvent()) {
                            continue;
                        }
                    }
                    if (!args.types.contains(CalendarArgs.TYPE_VACATION)) {
                        if (data.isFinanceVacation()) {
                            continue;
                        }
                    }
                }
                result.add(data);
            }
            return result;
        }
        return null;
    }

    public CalendarArgs getFilterArgs() {
        if (mDatas != null && !mDatas.isEmpty()) {
            CalendarArgs args = new CalendarArgs();
            for (CalendarRequest.CalendarData data : mDatas) {
                if (data == null) {
                    continue;
                }
                if (!TextUtils.isEmpty(data.country)) {
                    if (args.countries == null || !args.countries.contains(data.country)) {
                        args.addCountry(data.country);
                    }
                }
            }
            return args;
        }
        return null;
    }

}
